package org.ant.gui.menu;

import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import org.ant.gui.actionListeners.ExitActionListener;
import org.ant.gui.actionListeners.NewActionListener;
import org.ant.gui.actionListeners.OpenActionListener;
import org.ant.gui.actionListeners.PropertiesActionListener;
import org.ant.gui.actionListeners.SaveActionListener;
import org.ant.gui.actionListeners.SaveAsActionListener;
import org.ant.gui.actionListeners.ThreeAddressActionListener;

public class FileMenu extends JMenu {

	public static final String NAME = "File";
	public static final String ITEM_NEW = "New";
	public static final String ITEM_OPEN = "Open";
	public static final String ITEM_SAVE = "Save";
	public static final String ITEM_SAVE_AS = "Save As...";
	public static final String ITEM_TAC_SAVE = "Save TAC";
	public static final String ITEM_PROPERTIES = "Properties";
	public static final String ITEM_EXIT = "Exit";

	private JMenuItem newItem;
	private JMenuItem openItem;
	private JMenuItem saveItem;
	private JMenuItem saveAsItem;
	private JMenuItem tacItem;
	private JMenuItem propertiesItem;
	private JMenuItem exitItem;

	public FileMenu() {
		super();
		setText(NAME);

		add(getNewItem());
		add(getOpenItem());

		addSeparator();

		add(getSaveItem());
		add(getSaveAsItem());
		add(getTACItem());

		addSeparator();

		add(getPropertiesItem());

		addSeparator();

		add(getExitItem());
	}

	public JMenuItem getNewItem() {
		if (newItem == null) {
			newItem = new JMenuItem();
			newItem.setText(ITEM_NEW);
			Icon icon = new ImageIcon("icon/new.gif");
			newItem.setIcon(icon);
			newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
					InputEvent.CTRL_MASK));
			newItem.addActionListener(new NewActionListener());
		}
		return newItem;
	}

	public JMenuItem getOpenItem() {
		if (openItem == null) {
			openItem = new JMenuItem();
			openItem.setText(ITEM_OPEN);
			Icon icon = new ImageIcon("icon/open.gif");
			openItem.setIcon(icon);
			openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
					InputEvent.CTRL_MASK));
			openItem.addActionListener(new OpenActionListener());

		}
		return openItem;
	}

	public JMenuItem getSaveItem() {
		if (saveItem == null) {
			saveItem = new JMenuItem();
			saveItem.setText(ITEM_SAVE);
			Icon icon = new ImageIcon("icon/save.gif");
			saveItem.setIcon(icon);
			saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
					InputEvent.CTRL_MASK));
			saveItem.addActionListener(new SaveActionListener());
		}
		return saveItem;
	}

	public JMenuItem getSaveAsItem() {
		if (saveAsItem == null) {
			saveAsItem = new JMenuItem();
			Icon icon = new ImageIcon("icon/saveas.gif");
			saveAsItem.setIcon(icon);
			saveAsItem.setText(ITEM_SAVE_AS);
			saveAsItem.addActionListener(new SaveAsActionListener());
		}
		return saveAsItem;
	}

	public JMenuItem getTACItem() {
		if (tacItem == null) {
			tacItem = new JMenuItem();
			Icon icon = new ImageIcon("icon/tac.gif");
			tacItem.setIcon(icon);
			tacItem.setText(ITEM_TAC_SAVE);
			tacItem.addActionListener(new ThreeAddressActionListener());
		}
		return tacItem;
	}
	
	public JMenuItem getPropertiesItem() {
		if (propertiesItem == null) {
			propertiesItem = new JMenuItem();
			propertiesItem.setText(ITEM_PROPERTIES);
			Icon icon = new ImageIcon("icon/properties.gif");
			propertiesItem.setIcon(icon);
			propertiesItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
					InputEvent.ALT_MASK));
			propertiesItem.addActionListener(new PropertiesActionListener());
		}
		return propertiesItem;
	}

	public JMenuItem getExitItem() {
		if (exitItem == null) {
			exitItem = new JMenuItem();
			Icon icon = new ImageIcon("icon/exit.gif");
			exitItem.setIcon(icon);
			exitItem.setText(ITEM_EXIT);
			exitItem.addActionListener(new ExitActionListener());
		}
		return exitItem;
	}
}
